package org.geektimes.configuration.microprofile.config.source.servlet;

import org.geektimes.configuration.microprofile.config.source.MapBasedConfigSource;

import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class ServletContextConfigSource extends MapBasedConfigSource {

    private ServletContext servletContext;

    static CountDownLatch countDownLatch = new CountDownLatch(1);

    public ServletContextConfigSource(ServletContext servletContext){
        super("servletContext init parameters", 500,countDownLatch);
        this.servletContext = servletContext;
        countDownLatch.countDown();
    }

    @Override
    protected void prepareConfigData(Map configData) throws Throwable {
        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String parameterName = initParameterNames.nextElement();
            configData.put(parameterName,servletContext.getInitParameter(parameterName));
        }
    }
}
